<?php
class Cms_Model_Banner
{
	private static $_instance;
	private static $_cacheTimeout = 300;
	
	private function __construct() {
	}
	
	/**
	 * @return Cms_Model_Banner
	 * */
	public static function factory() {
		if(self::$_instance == null) {
			self::$_instance = new self();
		}
		
		return self::$_instance;
	}
	
	/**
	 * Get banner by given position
	 * @param integer|array $position
	 * @return array
	 */
	public function getBanner($position, $limit=1, $offset=0) {
		if(!$position) {
			return array();
		}
		
		$options = array('position' => $position, 'limit' => $limit, 'offset' => $offset);
		$bannerDao = Cms_Model_DAO_Banner::factory();
		try {
			$result = $bannerDao->getBanner($options);
		} catch (Exception $exc) {
			prBinh($exc);
			Utils_Global::storeLog($exc, __FILE__, __LINE__ );
		}
		
		return $result;
	}
	
	/**
	 * Get banner count by given position
	 * @param integer $position
	 * @return integer
	 * */
	public function getBannerCount($position) {
	    if(!$position) {
	    	return 0;
	    }
	    
	    $options = array('position' => $position);
	    $bannerDao = Cms_Model_DAO_Banner::factory();
	    try {
	    	$result = $bannerDao->getBannerCount($options);
	    } catch (Exception $exc) {
	    	prBinh($exc);
	    	Utils_Global::storeLog($exc, __FILE__, __LINE__ );
	    }
	    
	    return $result;
	}
	
	/**
	 * Get album
	 * */
	public function getAlbums($limit=1, $offset=0) {
	    $albums = array();
	    $options = array('limit' => $limit, 'offset' => $offset);
		$bannerDao = Cms_Model_DAO_Banner::factory();
		try {
			$result = $bannerDao->getAlbums($options);
			if(is_array($result) && count($result) > 0) {
			    foreach ($result as $item) {
			        if($albums[$item['id']]) {
			            $albums[$item['id']]['images'][] = array('image' => $item['image'], 'name' => $item['name'], 
			                                                        'width' => $item['width'], 'height' => $item['height']);
			        } else {
			            $albums[$item['id']] = array('id' => $item['id'], 'name' => $item['album_name'], 'image' => $item['album_image'], 'images' => array());
			            $albums[$item['id']]['images'][] = array('image' => $item['image'], 'name' => $item['name'], 
			                                                        'width' => $item['width'], 'height' => $item['height']);
			        }
			    }
			}
		} catch (Exception $exc) {
			prBinh($exc);
			Utils_Global::storeLog($exc, __FILE__, __LINE__ );
		}
		return $albums;
	}
	
	public function getAlbumCount() {
	    $bannerDao = Cms_Model_DAO_Banner::factory();
	    try {
	    	$result = $bannerDao->getAlbumCount();
	    } catch (Exception $exc) {
	    	prBinh($exc);
	    	Utils_Global::storeLog($exc, __FILE__, __LINE__ );
	    }
	     
	    return $result;
	}
}